/*
 * @(#)org.example 2020/3/22/0022
 *
 * Copy Right@ 人人译视界
 */


package org.example;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

import java.io.UnsupportedEncodingException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

/**
 * <pre>
 * @author tuwei
 *
 * @version 0.9
 *
 * 修改版本: 0.9
 * 修改日期: 2020/3/22/0022
 * 修改人 :  tuwei
 * 修改说明: 初步完成
 * 复审人 ：
 * </pre>
 */
public class RpcServerHandler extends ChannelInboundHandlerAdapter {
	private Map<String, Object> handlerMap = new HashMap<>();

	public RpcServerHandler(Map<String, Object> handlerMap) {
		this.handlerMap = handlerMap;
	}

	@Override
	public void channelActive(ChannelHandlerContext ctx) throws UnsupportedEncodingException {
		System.out.println("channelActive:" + ctx.channel().remoteAddress());
	}

	@Override
	public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
		System.out.println("服务端接收到消息：" + msg);
		RpcRequest rpcRequest = (RpcRequest) msg;
		Object result = new Object();
		if (handlerMap.containsKey(rpcRequest.getClassName())) {
			Object clazz = handlerMap.get(rpcRequest.getClassName());
			Method method = clazz.getClass().getMethod(rpcRequest.getMethodName(), rpcRequest.getTypes());
			result = method.invoke(clazz, rpcRequest.getParams());
		}
		ctx.write(result);
		ctx.flush();
		ctx.close();
	}
}
